Skip to content

Method: createNativeQuery(String, Class)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.query.sparql;
19:
20: import cz.cvut.kbss.jopa.model.QueryImpl;
21: import cz.cvut.kbss.jopa.model.ResultSetMappingQuery;
22: import cz.cvut.kbss.jopa.model.TypedQueryImpl;
23: import cz.cvut.kbss.jopa.query.QueryParser;
24: import cz.cvut.kbss.jopa.query.mapper.SparqlResultMapper;
25: import cz.cvut.kbss.jopa.query.parameter.ParameterValueFactory;
26: import cz.cvut.kbss.jopa.query.soql.SoqlQueryParser;
27: import cz.cvut.kbss.jopa.sessions.ConnectionWrapper;
28: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
29:
30: import java.util.Objects;
31:
32: /**
33: * Factory for creating SPARQL queries.
34: */
35: public class SparqlQueryFactory {
36:
37: private final UnitOfWork uow;
38: private final ConnectionWrapper connection;
39:
40: private final QueryParser queryParser;
41: private final SoqlQueryParser soqlQueryParser;
42:
43: public SparqlQueryFactory(UnitOfWork uow, ConnectionWrapper connection) {
44: assert uow != null;
45: assert connection != null;
46: this.uow = uow;
47: this.connection = connection;
48: this.queryParser = new SparqlQueryParser(new ParameterValueFactory(uow));
49: this.soqlQueryParser = new SoqlQueryParser(queryParser, uow.getMetamodel());
50: }
51:
52: /**
53: * Creates query object representing a native SPARQL query.
54: *
55: * @param sparql The query
56: * @return Query object
57: * @throws NullPointerException If {@code sparql} is {@code null}
58: */
59: public QueryImpl createNativeQuery(String sparql) {
60: Objects.requireNonNull(sparql);
61:
62: return new QueryImpl(queryParser.parseQuery(sparql), connection);
63: }
64:
65: /**
66: * Creates typed query object representing a native SPARQL query.
67: *
68: * @param sparql The query
69: * @param resultClass Type of the results
70: * @return Query object
71: * @throws NullPointerException If {@code sparql} or {@code resultClass} is {@code null}
72: */
73: public <T> TypedQueryImpl<T> createNativeQuery(String sparql, Class<T> resultClass) {
74: Objects.requireNonNull(sparql);
75:
76: return createQueryImpl(sparql, resultClass, queryParser);
77: }
78:
79: private <T> TypedQueryImpl<T> createQueryImpl(String query, Class<T> resultClass, QueryParser parser) {
80: Objects.requireNonNull(resultClass);
81:
82: return new TypedQueryImpl<>(parser.parseQuery(query), resultClass, connection, uow);
83: }
84:
85: /**
86: * Creates a query object representing a native SPARQL query.
87: *
88: * @param sparql The query
89: * @param resultSetMapping Name of the result set mapping to apply
90: * @return Query object * @throws NullPointerException If {@code sparql} or {@code resultSetMapping} is {@code null}
91: */
92: public QueryImpl createNativeQuery(String sparql, String resultSetMapping) {
93: Objects.requireNonNull(sparql);
94: Objects.requireNonNull(resultSetMapping);
95:
96: final SparqlResultMapper mapper = uow.getResultSetMappingManager().getMapper(resultSetMapping);
97: return new ResultSetMappingQuery(queryParser.parseQuery(sparql), connection, mapper, uow);
98: }
99:
100: /**
101: * Creates query object representing a native SPARQL query.
102: *
103: * @param query The query
104: * @return Query object
105: * @throws NullPointerException If {@code sparql} is {@code null}
106: */
107: public QueryImpl createQuery(String query) {
108: Objects.requireNonNull(query);
109:
110: return new QueryImpl(soqlQueryParser.parseQuery(query), connection);
111: }
112:
113: /**
114: * Creates typed query object representing a native SPARQL query.
115: *
116: * @param query The query
117: * @param resultClass Type of the results param URI of the ontology context against which the query will be
118: * evaluated
119: * @return Query object
120: * @throws NullPointerException If {@code sparql} or {@code resultClass} is {@code null}
121: */
122: public <T> TypedQueryImpl<T> createQuery(String query, Class<T> resultClass) {
123: Objects.requireNonNull(query);
124: return createQueryImpl(query, resultClass, soqlQueryParser);
125: }
126:
127: /**
128: * Creates a query object representing a native SPARQL query.
129: *
130: * @param name The name of the query defined in metadata
131: * @return Query object
132: * @throws IllegalArgumentException If a query has not been defined with the given name
133: */
134: public QueryImpl createNamedQuery(String name) {
135: final String query = uow.getNamedQueryManager().getQuery(name);
136: return createNativeQuery(query);
137: }
138:
139: /**
140: * Creates a typed query object representing a native SPARQL query.
141: *
142: * @param name The name of the query defined in metadata
143: * @param resultClass Type of the results param URI of the ontology context against which the query will be
144: * evaluated
145: * @return Query object
146: * @throws IllegalArgumentException If a query has not been defined with the given name
147: */
148: public <T> TypedQueryImpl<T> createNamedQuery(String name, Class<T> resultClass) {
149: final String query = uow.getNamedQueryManager().getQuery(name);
150: return createNativeQuery(query, resultClass);
151: }
152: }